package org.nyt.medicalstore.controller;

import org.apache.coyote.BadRequestException;
import org.nyt.medicalstore.dto.DrugSimilarityDTO;
import org.nyt.medicalstore.dto.SymptomDTO;
import org.nyt.medicalstore.dto.SymptomDrugRelationDTO;
import org.nyt.medicalstore.model.Drug;
import org.nyt.medicalstore.model.DrugSimilarity;
import org.nyt.medicalstore.model.Symptom;
import org.nyt.medicalstore.model.SymptomDrugRelation;
import org.nyt.medicalstore.service.DrugService;
import org.nyt.medicalstore.service.DrugSimilarityService;
import org.nyt.medicalstore.service.SymptomDrugRelationService;
import org.nyt.medicalstore.service.SymptomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/relation")
public class RelationController {

    @Autowired
    private SymptomDrugRelationService symptomDrugRelationService;

    @Autowired
    private DrugSimilarityService drugSimilarityService;

    @Autowired
    private SymptomService symptomService;

    @Autowired
    private DrugService drugService;

    private DrugSimilarityDTO toDto(DrugSimilarity drugSimilarity){
        if (drugSimilarity==null){
            return null;
        }
        DrugSimilarityDTO dto = new DrugSimilarityDTO();
        dto.setDrug1Id(drugSimilarity.getDrug1().getId());
        dto.setDrug2Id(drugSimilarity.getDrug2().getId());
        dto.setScore(drugSimilarity.getSimilarity());
        return dto;
    }

    private SymptomDrugRelationDTO toDto(SymptomDrugRelation symptomDrugRelation){
        if (symptomDrugRelation==null){
            return null;
        }
        SymptomDrugRelationDTO dto = new SymptomDrugRelationDTO();
        dto.setDrugId(symptomDrugRelation.getDrug().getId());
        dto.setSymptomId(symptomDrugRelation.getSymptom().getId());
        dto.setConfidence(symptomDrugRelation.getConfidence());
        return dto;
    }

    // 获取相似药品
    @GetMapping("/drug/{id}")
    public List<Drug> getSimilarDrugs(
            @PathVariable Long id,
            @RequestParam(required = false) Double minScore) {
        return drugSimilarityService.getSimilarDrugs(id, minScore);
    }

    // 添加药品相似关系
    @PostMapping("/drug/similarity")
    public DrugSimilarityDTO addDrugSimilarity(@RequestBody DrugSimilarity drugSimilarity) throws BadRequestException {
        DrugSimilarity similarity = drugSimilarityService.addSimilarity(drugSimilarity.getDrug1().getId(), drugSimilarity.getDrug2().getId(), drugSimilarity.getSimilarity());
        return toDto(similarity);
    }

    // 获取症状相关药品
    @GetMapping("/symptom/{id}")
    public List<Drug> getRelatedDrugs(@PathVariable Long id) {
        return symptomDrugRelationService.getRelatedDrugs(id);
    }

    // 添加症状-药品关系
    @PostMapping("/symptom/relation")
    public SymptomDrugRelationDTO addSymptomDrugRelation(@RequestBody SymptomDrugRelation symptomDrugRelation) throws BadRequestException {
        SymptomDrugRelation relation = symptomDrugRelationService.addSymptomDrugRelation(
                symptomDrugRelation.getSymptom().getId(), symptomDrugRelation.getDrug().getId(), symptomDrugRelation.getConfidence());
        return toDto(relation);
    }
}
